package com.fulihui.information.core.redis;

import com.fulihui.information.core.db.CommentsOnOffDB;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by LM on 2017/4/12.
 */
@Component
public class RedisCommentsOnOffDB implements CommentsOnOffDB {

    /**
     * 评论开关key
     */
    private static final String ON_OFF_KEY = "CONTENT_COMMENTS:ON_OFF";
    @Autowired
    private JedisPool jedisPool;

    @Override
    public boolean onOff() {
        try (Jedis jedis = jedisPool.getResource()) {
            String value = jedis.get(ON_OFF_KEY);
            return !(StringUtil.isBlank(value) || Integer.parseInt(value) == 0);
        }
    }

    @Override
    public void openOnOff() {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(ON_OFF_KEY, "1");
        }
    }

    @Override
    public void closeOnOff() {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(ON_OFF_KEY, "0");
        }
    }
}
